﻿using System.Collections.Generic;
using System.Linq;
using DevExpress.Data.Filtering;
using DevExpress.Data.Filtering.Helpers;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Registrator;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.Drawing;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace Cs.ControlCenter.Uc.Bases
{
    public class CustomGridView : GridView
    {
        protected override string ViewName
        {
            get { return "CustomGridView"; }
        }

        protected internal virtual string GetExtraFilterText
        {
            get { return ExtraFilterText; }
        }

        protected internal virtual void SetGridControlAccessMetod(GridControl newControl)
        {
            SetGridControl(newControl);
        }

        protected override string OnCreateLookupDisplayFilter(string text, string displayMember)
        {
            List<CriteriaOperator> subStringOperators = (text.Split(' ')
                .Select(sString => LikeData.CreateContainsPattern(sString))
                .Select(exp => (from GridColumn col in Columns
                    where col.Visible && col.ColumnType == typeof (string)
                    select new BinaryOperator(col.FieldName, exp, BinaryOperatorType.Like)).Cast<CriteriaOperator>()
                    .ToList()).Select(columnsOperators => new GroupOperator(GroupOperatorType.Or, columnsOperators))).Cast<CriteriaOperator>().ToList();
            return new GroupOperator(GroupOperatorType.And, subStringOperators).ToString();
        }}

    public class CustomGridControl : GridControl
    {
        protected override void RegisterAvailableViewsCore(InfoCollection collection)
        {
            base.RegisterAvailableViewsCore(collection);
            collection.Add(new CustomGridInfoRegistrator());
        }

        protected override BaseView CreateDefaultView()
        {
            return CreateView("CustomGridView");
        }
    }

    public class CustomGridPainter : GridPainter
    {
        public CustomGridPainter(GridView view) : base(view)
        {
        }

        public new virtual CustomGridView View
        {
            get { return (CustomGridView) base.View; }
        }

        protected override void DrawRowCell(GridViewDrawArgs e, GridCellInfo cell)
        {
            cell.ViewInfo.MatchedStringUseContains = true;
            cell.ViewInfo.MatchedString = View.GetExtraFilterText;
            cell.State = GridRowCellState.Dirty;
            e.ViewInfo.UpdateCellAppearance(cell);
            base.DrawRowCell(e, cell);
        }
    }

    public class CustomGridInfoRegistrator : GridInfoRegistrator
    {
        public override string ViewName
        {
            get { return "CustomGridView"; }
        }

        public override BaseViewPainter CreatePainter(BaseView view)
        {
            return new CustomGridPainter(view as GridView);
        }

        public override BaseView CreateView(GridControl grid)
        {
            var view = new CustomGridView();
            view.SetGridControlAccessMetod(grid);
            return view;
        }
    }
}